package com.sprint.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.sprint.entities.CommonResult;
import com.sprint.entities.Payment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RateLimitController {

    @GetMapping("/byResource")
    @SentinelResource(value = "byResouce", blockHandler = "handleException")
    public CommonResult byResource()
    {
        return new CommonResult(200,"按资源限流成功",new Payment(2020L,"xxxx"));
    }

    public CommonResult handleException(BlockException exception){
        return new CommonResult(444,exception.getClass().getCanonicalName()+"\t 服务不可用");
    }


    @GetMapping("/byUrl")
    @SentinelResource(value = "byUrl")
    public CommonResult byUrl()
    {
        return new CommonResult(200,"按资源限流URL成功",new Payment(2020L,"xxxx"));
    }

    @GetMapping("/bycustomUrl")
    @SentinelResource(value = "bycustomUrl",fallbackClass = FLowLimitController.class
        ,fallback = "xxxx")
    public CommonResult customExceptiondeal()
    {
        return new CommonResult(200,"按资源限流customExceptiondeal成功",new Payment(2020L,"xxxx"));
    }

}
